package net.zz.base.controller;

import net.zz.base.exception.CommonException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by ZaoSheng on 2015/5/9.
 */
@Controller
public abstract class SupportController {


    private static final Logger LOG = LoggerFactory.getLogger(SupportController.class);



	
    protected Map<String,Object> successData() {
        Map<String,Object> data = newData("ZZCode", 0);
        return  data;
    }
    protected Map<String,Object> successData(String key, Object value) {
        Map<String,Object> data = newData("ZZCode", 0);
        data.put(key, value);
        return  data;
    }
    
    protected Map<String,Object> successData(Object MZCode) {
    	 Map<String,Object> data = newData("MZCode", MZCode);
         return  data;
    }
    
    
    protected Map<String,Object> newData(String key, Object value) {
    	Map<String,Object> data = newData();
    	data.put(key, value);
    	return data;
    }
    
    
    protected Map<String,Object> newData() {
        Map<String,Object> data = new HashMap<String, Object>();
        return data;
    }



    protected void throwError(Integer code, String message) throws Exception {
        throw new CommonException(code, message);
    }

    protected void throwError(Integer code) throws Exception {
        throwError(code, "");
    }

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Map<String,Object> handleException(Exception ex) throws Exception {
        Map<String,Object> data = newData();
        System.out.println("------handleException---------------");
        if (ex instanceof CommonException) {
            CommonException m = (CommonException) ex ;
            data.put("ZZCode", m.getCode());
            data.put("message", m.getMessage());
        } else {
//            throw ex;
            data.put("ZZCode",11);
            data.put("message",ex.getMessage());
        }
        
        return data;
    }

}
